package com.hotel.BL;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.MessagePOS;

public class POSMessagesLogic extends AbstractBL {
	public POSMessagesLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public MessagePOS AddMessageFolioPOS(long hotelId,String txtMessage,String thxMessage) throws EntityNotFoundException{
		MessagePOS b = new MessagePOS();
		b.setHotelId(hotelId);
		b.setMessage(txtMessage);
		b.setThxmessage(thxMessage);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	public List<MessagePOS> GetMessagePOSByhotelId(long hotelId) {
		Query q = new Query(EntityName.MessagePOS);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		q.setFilter(filterHotel);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public MessagePOS EditMessagePOS(long id,String txtMessage,String thxMessage) throws EntityNotFoundException{
		MessagePOS b =  (MessagePOS) GetMessagePOS(id);
		b.setMessage(txtMessage);
		b.setThxmessage(thxMessage);
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	
	public MessagePOS GetMessagePOS(long id)
	{
		Key key = KeyFactory.createKey(EntityName.MessagePOS, id);
		try {
			Entity lang = datastore.get(key);
			MessagePOS c = new MessagePOS();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<MessagePOS> GetList(List<Entity> entities) {
		List<MessagePOS> list = new ArrayList<MessagePOS>();
		if(entities!=null)
		for (Entity e : entities) {
			MessagePOS a = new MessagePOS();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
}
